package com.gery.tn.ui;

import com.gery.tn.backend.model.Appointment;
import com.gery.tn.backend.service.DAOFactory;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.jdesktop.swingx.JXMonthView;




public class AppointmentPanel extends JPanel {
    
    private JLabel separator;
    private JLabel nameLabel;
    private long id;
    private JXMonthView datePicker;
    private ImageIcon img;
    private JTextField appHour;
    private JTextField appMinute;
    private JTextField appDetails;
    private JLabel dev;
    private JButton saveButton;
    private JButton cancelButton;
    private ImageIcon saveButtonNormal;
    private ImageIcon saveButtonHover;
    private ImageIcon saveButtonDown;
    private ImageIcon cancelButtonNormal;
    private ImageIcon cancelButtonHover;
    private ImageIcon cancelButtonDown;
    private Calendar appDate;
    
    
    
    public AppointmentPanel() {
        this.setBounds(0, 255, 241, 275);
        this.setBackground(new Color(54, 62, 65));
        this.setBorder(null);
        this.setLayout(null);
        this.setVisible(false);
    
        img = new ImageIcon(this.getClass().getClassLoader().getResource("com/gery/tn/ui/images/buttonSeparator.png"));
        separator = new JLabel(img);
        separator.setBounds(0, 35, 241, 5);
        
        nameLabel = new JLabel();
        nameLabel.setBounds(6, 7, 230, 20);
        nameLabel.setFont(new Font("Apple", Font.BOLD, 14));
        nameLabel.setHorizontalAlignment(JLabel.CENTER);
        nameLabel.setForeground(Color.WHITE);
        
        datePicker = new JXMonthView();
        datePicker.setBounds(0, 40, 241, 145);
        datePicker.setBackground(new Color(114, 166, 234));
        datePicker.setFont(new Font("Apple", Font.PLAIN, 12));
        datePicker.setBoxPaddingX(1);
        datePicker.setBoxPaddingY(0);
        datePicker.setFirstDayOfWeek(2);
        datePicker.setTraversable(true);
        datePicker.setSelectionDate(new Date());
        datePicker.setSelectionBackground(Color.LIGHT_GRAY);
        datePicker.setMonthStringBackground(new Color(114, 166, 234));
        datePicker.setDaysOfTheWeekForeground(Color.WHITE);
        
        appHour = new JTextField("HH");
        appHour.setBounds(0, 185, 115, 30);
        appHour.setBorder(null);
        appHour.setForeground(Color.WHITE);
        appHour.setBackground(new Color(114, 166, 234));
        appHour.setFont(new Font("Apple", Font.BOLD, 16));
        appHour.setHorizontalAlignment(JLabel.CENTER);
        
        appMinute = new JTextField("MM");
        appMinute.setBounds(125, 185, 121, 30);
        appMinute.setBorder(null);
        appMinute.setForeground(Color.WHITE);
        appMinute.setBackground(new Color(114, 166, 234));
        appMinute.setFont(new Font("Apple", Font.BOLD, 16));
        appMinute.setHorizontalAlignment(JLabel.CENTER);
        
        dev = new JLabel(":");
        dev.setBounds(115, 185, 10, 30);
        dev.setOpaque(true);
        dev.setBackground(new Color(114, 166, 234));
        dev.setForeground(Color.WHITE);
        dev.setFont(new Font("Apple", Font.BOLD, 16));
        
        appDetails = new JTextField(" Details...");
        appDetails.setBounds(0, 215, 241, 30);
        appDetails.setBorder(null);
        appDetails.setFont(new Font("Apple", Font.ITALIC, 12));
        appDetails.setForeground(Color.WHITE);
        appDetails.setBackground(new Color(54, 62, 65));
        
        saveButtonNormal = new ImageIcon(this.getClass().getClassLoader().getResource("com/gery/tn/ui/images/saveFastAppointmentNormal.png"));
        saveButtonHover = new ImageIcon(this.getClass().getClassLoader().getResource("com/gery/tn/ui/images/saveFastAppointmentHover.png"));
        saveButtonDown = new ImageIcon(this.getClass().getClassLoader().getResource("com/gery/tn/ui/images/saveFastAppointmentDown.png"));
        saveButton = new JButton(saveButtonNormal);
        saveButton.setRolloverIcon(saveButtonHover);
        saveButton.setPressedIcon(saveButtonDown);        
        saveButton.setBorder(null);
        saveButton.setBounds(0, 245, 121, 30);
        
        cancelButtonNormal = new ImageIcon(this.getClass().getClassLoader().getResource("com/gery/tn/ui/images/cancelFastAppointmentNormal.png"));
        cancelButtonHover = new ImageIcon(this.getClass().getClassLoader().getResource("com/gery/tn/ui/images/cancelFastAppointmentHover.png"));
        cancelButtonDown = new ImageIcon(this.getClass().getClassLoader().getResource("com/gery/tn/ui/images/cancelFastAppointmentDown.png"));
        cancelButton = new JButton(cancelButtonNormal);
        cancelButton.setRolloverIcon(cancelButtonHover);
        cancelButton.setPressedIcon(cancelButtonDown);
        cancelButton.setBorder(null);
        cancelButton.setBounds(121, 245, 120, 30);
        
        this.add(separator);
        this.add(nameLabel);
        this.add(datePicker);
        this.add(appHour);
        this.add(dev);
        this.add(appMinute);
        this.add(appDetails);
        this.add(saveButton);
        this.add(cancelButton);
        
        
        
        //--- Event handling
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                
            }
        });
        datePicker.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) appHour.requestFocus();
            }
        });
        appHour.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                appHour.setText("");
            }
        });
        appMinute.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                appMinute.setText("");
            }
        });
        appHour.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                if (appHour.getText().length() == 2) appMinute.requestFocus();
            }
        });
        appMinute.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                if (appMinute.getText().length() == 2) {
                    appDetails.setText("");
                    appDetails.requestFocus();
                }
            }
        });
        appDetails.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                appDetails.setText("");
            }
        });
        appDetails.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    saveData();
                }
            }
        });
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panelOff();
            }
        });
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                saveData();
            }
        });
        
        
    }
    
    
    public void saveData() {
        initDate();
        Appointment app = new Appointment(appDate, appDetails.getText());
        DAOFactory.getInstance().getAppointmentService().addAppointment(id, app);
        panelOff();
    }
    
    public void panelOn(long id, String name) {
        this.id = id;
        nameLabel.setText(name); 
        this.setVisible(true);
    }
    public void panelOff() {
        datePicker.setSelectionDate(new Date());
        appHour.setText("HH");
        appMinute.setText("MM");
        appDetails.setText(" Details...");
        this.setVisible(false);
    }
    public void setPickerFocus() {
        datePicker.requestFocus();
    }
    public void initDate() {
        appDate = GregorianCalendar.getInstance();
        appDate.setTime(datePicker.getSelectionDate());
        appDate.set(Calendar.HOUR_OF_DAY, Integer.parseInt(appHour.getText()));
        appDate.set(Calendar.MINUTE, Integer.parseInt(appMinute.getText()));
    }
    
}
